﻿using System;
using System.Collections.Generic;
using System.Linq;
using GPSTracker.API.DataModel;
using GPSTracker.API.Helper;

namespace GPSTracker.API.DAL
{
    public class LoginDAL
    {
        public static DataModel.SP_CheckLoginByTempoResult CheckLoginByTempo(string loginName, string loginPwd, string callFrom)
        {
            try
            {
                var context = new DataModel.GpsDCDataContext(CommonFx.GetConStr());
                return context.SP_CheckLoginByTempo(loginName, loginPwd, callFrom).SingleOrDefault();
            }
            catch (Exception ex)
            {
                var el = new ErrorLog();
                el.ErrorMessage = ex.Message;
                el.ErrorForm = "LoginDAL";
                el.ErrorFunction = "CheckLoginByTempo";
                CommonDAL.AddErrorLog(el);

                return null;
            }
        }

        public static DataModel.SP_CheckLoginResult CheckLoginByMobile(string loginName, string loginPwd, string callFrom)
        {
            try
            {
                var context = new DataModel.GpsDCDataContext(CommonFx.GetConStr());

                bool? IsErrorFound = true;
                string ErrorMessage = String.Empty;

                var login = context.SP_CheckLogin(loginName, loginPwd, "MOBILE", callFrom, ref IsErrorFound, ref ErrorMessage).SingleOrDefault();

                if (IsErrorFound == false)
                {
                    return login;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                var el = new ErrorLog();
                el.ErrorMessage = ex.Message;
                el.ErrorForm = "LoginDAL";
                el.ErrorFunction = "CheckLoginByMobile";
                CommonDAL.AddErrorLog(el);

                return null;
            }
        }
    }
}
